package org.mini.utils
{
	import com.shine.manager.StageManager;
	import com.shine.output.Output;
	
	import org.mini.config.AppConfig;
	import org.mini.main.Application;
	
	/**
	 * output the message
	 * 
	 * <p>usage：</p>
	 * <pre>
	 * Debugging.dubug("debug message");
	 * Debugging.info("info message");
	 * Debugging.warn("warn message");
	 * Debugging.error("error message");
	 * </pre>
	 * <p>Set Debugging Level：</p>
	 * <pre>
	 * Debugging.setLevel(com.msn.utils.log.LogLevel.WARN);
	 * </pre>
	 */	
	public class Debuger
	{
		/**
		* Debugging level value. 
		* @default LogLevel.ALL
		*/		
		static private var LOG_LEVEL:uint = LogLevel.ALL;
		
		static public var isDebugMode:Boolean=false;
		
		/** Log a message */
		public static function log(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.LOG)  pop("[LOG] : " + getTime() + args.join(" "),LogLevel.LOG);
		}
		
		/** Debug a message */
		public static function output(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.DEBUG) pop("[DEBUG] : " + getTime() + args.join(" "),LogLevel.DEBUG);
		}
		/** Info a message */
		public static function info(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.INFO) pop("[INFO] : " + getTime() + args.join(" "),LogLevel.INFO);
		}
		/** warn a message */
		public static function warn(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.WARN) pop("[WARN] : " + getTime() + args.join(" "),LogLevel.WARN);
		}
		/** error a message */
		public static function error(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.ERROR) pop("[ERROR] : " + getTime() + args.join(" "),LogLevel.ERROR);
		}
		/** fatal a message */
		public static function fatal(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.FATAL) pop("[FATAL] : " + getTime() + args.join(" "),LogLevel.FATAL);
		}
		/** load a message */
		public static function load(...args)	:void { 
			if(LOG_LEVEL <= LogLevel.LOAD) pop(args.join(" "),LogLevel.LOAD);
		}
		
		/**
		 * pop the message
		 * @param argument
		 * @param level
		 */		
		private static function pop(argument:String,level:uint = 1):void{
			if(isDebugMode) {
				Output.push(argument,level);
			}
			
			trace(argument);
		}
		
		private static function getTime():String
		{
			var d:Date = new Date();
			return d.toTimeString().slice(0,9);
		}
		
		private static function getDateTime():String
		{
			var d:Date = new Date();
			return d.toString();
		}
		/**
		 * Set the log Level. 
		 * @example
		 * <listing version="3.0" >Debugging.setLevel(LogLevel.ALL);</listing>
		 */
		public static function setLevel(level:uint):void
		{
			LOG_LEVEL = level;
		}
		
		public static function init():void
		{
			var output:Output=new Output(120, true);
			var version:String=Application.instance.getParam("version");
			output.setVersionText(version==null ? AppConfig.staticFileVersion : version);
			StageManager.getStage().addChild(output);
			StageManager.getStage().removeChild(output);
		}
	}
}